<?php

namespace common\helps;

use Yii;
use yii\web\Controller;

/*
 * 自定义全局公共方法
 */
class tools
{



    //ajax 正确返回
    public static function ajax_return($code = 0,$msg = '成功',$data = []){

        $array = ["code" => $code, "msg" => $msg, "data" => $data];

        return json_encode($array,JSON_UNESCAPED_UNICODE);
    }

    //ajx 错误返回
    public static function ajax_return_error($code = 1,$msg = '失败',$data = []){

        $array = ["code" => $code, "msg" => $msg, "data" => $data];

        return json_encode($array,JSON_UNESCAPED_UNICODE);
    }

    //假排序
    public static function fakeOrder($b,$a){
        $order = $b . sprintf("%04d", $a);
        return $order;
    }

    //查找上级
    public static function familyTree($depart,$code)
    {
        $query = new \yii\db\Query();
        $tree = array();
        while (!empty($code)) {
            $depart = $query
                ->select(['ID','departname','org_code','org_type'])
                ->from('ifssc_php.t_s_depart')
                ->where(['org_code'=>$code])
                ->one();
            $tree[] = $depart;
            $code = substr($depart['org_code'],0,-3);
            self::familyTree($depart, $code);


        }
        return $tree;
    }


    //token验证
    public static function checkToken($id,$token)
    {
        $redis = Yii::$app->cache;
        $accessToken = $redis->get($id);
        $tokenTime = substr($accessToken['token'],-10);
        $passTime =  7*24*3600;
        $time = time();
        if(empty($token)){
            return 1; //token不能为空
        }else if($tokenTime + $passTime < $time ){
            return 2;//token过期
        }else if($accessToken['token'] != $token ){
            return 3;//token不正确
        }else{
            return 4;
        }
    }


    //元转万
    public static function tenThousand($price){
        $p = bcdiv($price, 10000, 2);
        return $p;
    }

    //获取http
    public static function httpGet($url) {

        $ch = curl_init();
        //设置选项，包括URL
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        //执行并获取HTML文档内容
        $output = curl_exec($ch);
        //释放curl句柄
        curl_close($ch);
        //打印获得的数据
        return $output;
    }
    
    
    /**
     * title 生成uuid
     */
    public static function createUuid($prefix = ''){
            $chars = md5(uniqid(mt_rand(), true));
            $uuid  = substr($chars,0,8);
            $uuid .= substr($chars,8,4);
            $uuid .= substr($chars,12,4);
            $uuid .= substr($chars,16,4);
            $uuid .= substr($chars,20,12);
            return $prefix . $uuid;

    }


    /**
     * title 获取数据库名
     * @return string
     */
    public static function databaseName()
    {
        return 'data_yii';
    }


    /**
     * title 获取路径
     * @return string
     */
    public static function localUrl()
    {
        return '172.18.80.9:8087';
    }


    /**
     * 递归实现无限极分类
     * @param $array 分类数据
     * @param $pid 父ID
     * @param $level 分类级别
     * @return $list 分好类的数组 直接遍历即可 $level可以用来遍历缩进
     */

    //生成无限极分类树
    public static function getTree($arr){
        $refer = array();
        $tree = array();
        foreach($arr as $k => $v){
            $refer[$v['id']] = & $arr[$k]; //创建主键的数组引用
        }
        foreach($arr as $k => $v){
            $pid = $v['pid'];  //获取当前分类的父级id
            if($pid == 0){
                $tree[] = & $arr[$k];  //顶级栏目
            }else{
                if(isset($refer[$pid])){
                    $refer[$pid]['child'][] = & $arr[$k]; //如果存在父级栏目，则添加进父级栏目的子栏目数组中
                }
            }
        }
        return $tree;
    }


    /**
     * 递归获取各子级 @秋刀鱼
     * $departId 请求部门的ID
     * return array 各父级部门
     */
    public function getChildDeparts($array,$pid = 0){
        $childArr = array();
        foreach($array as $v) {
            if($v['pid']==$pid){
//                $v['level']=$level;
                $childArr[]=$v;
                $childArr = array_merge($childArr,$this->getChildDeparts($array,$v['ID']));
            }
        }
        return $childArr;
    }

    /**
     * 二维数组去重
     * @param $arr
     * @param $key
     * @return array
     */
    public static function array_unset_tt($arr,$key)
    {
        //建立一个目标数组
        $res = array();
        foreach ($arr as $value) {
            //查看有没有重复项

            if (isset($res[$value[$key]])) {
                //有：销毁

                unset($value[$key]);

            } else {

                $res[$value[$key]] = $value;
            }
        }
        return $res;


    }





}